package com.sqlquery.demo.controller;
import com.sqlquery.demo.entity.*;
import com.sqlquery.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.*;
@RestController
public class UserController {
    @Autowired
    private UserService userService;
    //增加用户
    @PutMapping("/emp/add")
    public String add(User user) {
        userService.addUser(user);
        return "增加用户成功";
    }
    // 通过用户id增加用户角色
    @PutMapping("/emp/addRoleById")
    public String addRoleById(User_Role user_role) {
        userService.addRoleById(user_role);
        return "增加成功";
    }
    //通过用户的角色id删除角色
    @DeleteMapping("/emp/delRoleById")
    public String delRoleById(User_Role user_role) {
        userService.delRoleById(user_role);
        return "删除成功";
    }
    //更新用户
    @PostMapping("/emp/update")
    public String update(User user) {
        userService.updateUser(user);
        return "修改成功";
    }
    // 根据名字查找信息  使用RESTful风格
    @GetMapping("/emp/findName/{name}")
    public List<Map<String, Object>> findByName(@PathVariable("name") String name) {
        return userService.findByName(name);
    }
    // 根据id查找信息
    @GetMapping("/emp/findId/{id}")
    public User findByName(@PathVariable("id") Integer id) {
        return userService.findById(id);
    }
    // 根据id删除信息
    @DeleteMapping("/emp/delete/{id}")
    public String deleteById(@PathVariable("id") Integer id) {
        userService.deleteId(id);
        return "删除成功";
    }
    //分页展示所有用户
    @GetMapping("/emp/{page}")
    public List<Map<String, Object>> findAll(@PathVariable("page") Integer page) {
        return userService.findAll(page);
    }
    // 根据用户名查询角色信息
    @GetMapping("/emp/findRoleByUserName/{name}")
    public List<Map<String, Object>> findRoleByUserName(@PathVariable("name") String name) {
        List<Map<String, Object>> role = userService.findRoleByUserName(name);
        return role;
    }
    // 根据角色名查询用户信息
    @GetMapping("/emp/findUserByRoleName/{name}")
    public List<Map<String, Object>> findUserByRoleName(@PathVariable("name") String name) {
        List<Map<String, Object>> role = userService.findUserByRoleName(name);
        return role;
    }
    // 根据用户id查询角色信息
    @GetMapping("/emp/findRoleByUserId/{id}")
    public List<Map<String, Object>> findRoleByUserId(@PathVariable("id") Integer id) {
        List<Map<String, Object>> role = userService.findRoleByUserId(id);
        return role;
    }
    // 根据角色id查询用户信息
    @GetMapping("/emp/findUserByRoleId/{id}")
    public List<Map<String, Object>> findUserByRoleId(@PathVariable("id") Integer id) {
        List<Map<String, Object>> user = userService.findUserByRoleId(id);
        return user;
    }
}
